package com.ctsi.ssdc.admin.repository;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

import com.ctsi.ssdc.admin.domain.CscpUserDetail;
import com.ctsi.ssdc.admin.domain.CscpUserDetailExample;
import com.ctsi.ssdc.admin.domain.CscpUserDetailLike;
import com.ctsi.ssdc.admin.domain.CscpUserDetailExample.Criteria;
import com.ctsi.ssdc.admin.domain.CscpUserDetailExample.Criterion;

/**
 * @author MyBatis Generator
*/
public class CscpUserDetailSqlProvider {

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table cscp_user_detail
     *
     * @mbg.generated Mon Apr 23 08:56:34 CST 2018
     */
    public String countByExample(CscpUserDetailExample example) {
        SQL sql = new SQL();
        sql.SELECT("count(*)").FROM("cscp_user_detail");
        applyWhere(sql, example, false);
        return sql.toString();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table cscp_user_detail
     *
     * @mbg.generated Mon Apr 23 08:56:34 CST 2018
     */
    public String deleteByExample(CscpUserDetailExample example) {
        SQL sql = new SQL();
        sql.DELETE_FROM("cscp_user_detail");
        applyWhere(sql, example, false);
        return sql.toString();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table cscp_user_detail
     *
     * @mbg.generated Mon Apr 23 08:56:34 CST 2018
     */
    public String insertSelective(CscpUserDetail record) {
        SQL sql = new SQL();
        sql.INSERT_INTO("cscp_user_detail");
        
        if (record.getUserId() != null) {
            sql.VALUES("user_id", "#{userId,jdbcType=INTEGER}");
        }
        
        if (record.getFamilyName() != null) {
            sql.VALUES("family_name", "#{familyName,jdbcType=VARCHAR}");
        }
        
        if (record.getName() != null) {
            sql.VALUES("name", "#{name,jdbcType=VARCHAR}");
        }
        
        if (record.getMobile() != null) {
            sql.VALUES("mobile", "#{mobile,jdbcType=VARCHAR}");
        }
        
        if (record.getEmail() != null) {
            sql.VALUES("email", "#{email,jdbcType=VARCHAR}");
        }
        
        if (record.getImgPath() != null) {
            sql.VALUES("img_path", "#{imgPath,jdbcType=VARCHAR}");
        }
        
        if (record.getLastLogin() != null) {
            sql.VALUES("last_login", "#{lastLogin,jdbcType=TIMESTAMP}");
        }
        
        if (record.getDiscTitle() != null) {
            sql.VALUES("disc_title", "#{discTitle,jdbcType=VARCHAR}");
        }
        
        if (record.getDiscDetail() != null) {
            sql.VALUES("disc_detail", "#{discDetail,jdbcType=VARCHAR}");
        }
        
        if (record.getRegisterTime() != null) {
            sql.VALUES("register_time", "#{registerTime,jdbcType=TIMESTAMP}");
        }
        
        return sql.toString();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table cscp_user_detail
     *
     * @mbg.generated Mon Apr 23 08:56:34 CST 2018
     */
    public String selectByExample(CscpUserDetailExample example) {
        SQL sql = new SQL();
        if (example != null && example.isDistinct()) {
            sql.SELECT_DISTINCT("id");
        } else {
            sql.SELECT("id");
        }
        sql.SELECT("user_id");
        sql.SELECT("family_name");
        sql.SELECT("name");
        sql.SELECT("mobile");
        sql.SELECT("email");
        sql.SELECT("img_path");
        sql.SELECT("last_login");
        sql.SELECT("disc_title");
        sql.SELECT("disc_detail");
        sql.SELECT("register_time");
        sql.FROM("cscp_user_detail");
        applyWhere(sql, example, false);
        
        if (example != null && example.getOrderByClause() != null) {
            sql.ORDER_BY(example.getOrderByClause());
        }
        
        return sql.toString();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table cscp_user_detail
     *
     * @mbg.generated Mon Apr 23 08:56:34 CST 2018
     */
    public String updateByExampleSelective(Map<String, Object> parameter) {
        CscpUserDetail record = (CscpUserDetail) parameter.get("record");
        CscpUserDetailExample example = (CscpUserDetailExample) parameter.get("example");
        
        SQL sql = new SQL();
        sql.UPDATE("cscp_user_detail");
        
        if (record.getId() != null) {
            sql.SET("id = #{record.id,jdbcType=INTEGER}");
        }
        
        if (record.getUserId() != null) {
            sql.SET("user_id = #{record.userId,jdbcType=INTEGER}");
        }
        
        if (record.getFamilyName() != null) {
            sql.SET("family_name = #{record.familyName,jdbcType=VARCHAR}");
        }
        
        if (record.getName() != null) {
            sql.SET("name = #{record.name,jdbcType=VARCHAR}");
        }
        
        if (record.getMobile() != null) {
            sql.SET("mobile = #{record.mobile,jdbcType=VARCHAR}");
        }
        
        if (record.getEmail() != null) {
            sql.SET("email = #{record.email,jdbcType=VARCHAR}");
        }
        
        if (record.getImgPath() != null) {
            sql.SET("img_path = #{record.imgPath,jdbcType=VARCHAR}");
        }
        
        if (record.getLastLogin() != null) {
            sql.SET("last_login = #{record.lastLogin,jdbcType=TIMESTAMP}");
        }
        
        if (record.getDiscTitle() != null) {
            sql.SET("disc_title = #{record.discTitle,jdbcType=VARCHAR}");
        }
        
        if (record.getDiscDetail() != null) {
            sql.SET("disc_detail = #{record.discDetail,jdbcType=VARCHAR}");
        }
        
        if (record.getRegisterTime() != null) {
            sql.SET("register_time = #{record.registerTime,jdbcType=TIMESTAMP}");
        }
        
        applyWhere(sql, example, true);
        return sql.toString();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table cscp_user_detail
     *
     * @mbg.generated Mon Apr 23 08:56:34 CST 2018
     */
    public String updateByExample(Map<String, Object> parameter) {
        SQL sql = new SQL();
        sql.UPDATE("cscp_user_detail");
        
        sql.SET("id = #{record.id,jdbcType=INTEGER}");
        sql.SET("user_id = #{record.userId,jdbcType=INTEGER}");
        sql.SET("family_name = #{record.familyName,jdbcType=VARCHAR}");
        sql.SET("name = #{record.name,jdbcType=VARCHAR}");
        sql.SET("mobile = #{record.mobile,jdbcType=VARCHAR}");
        sql.SET("email = #{record.email,jdbcType=VARCHAR}");
        sql.SET("img_path = #{record.imgPath,jdbcType=VARCHAR}");
        sql.SET("last_login = #{record.lastLogin,jdbcType=TIMESTAMP}");
        sql.SET("disc_title = #{record.discTitle,jdbcType=VARCHAR}");
        sql.SET("disc_detail = #{record.discDetail,jdbcType=VARCHAR}");
        sql.SET("register_time = #{record.registerTime,jdbcType=TIMESTAMP}");
        
        CscpUserDetailExample example = (CscpUserDetailExample) parameter.get("example");
        applyWhere(sql, example, true);
        return sql.toString();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table cscp_user_detail
     *
     * @mbg.generated Mon Apr 23 08:56:34 CST 2018
     */
    public String updateByPrimaryKeySelective(CscpUserDetail record) {
        SQL sql = new SQL();
        sql.UPDATE("cscp_user_detail");
        
        if (record.getUserId() != null) {
            sql.SET("user_id = #{userId,jdbcType=INTEGER}");
        }
        
        if (record.getFamilyName() != null) {
            sql.SET("family_name = #{familyName,jdbcType=VARCHAR}");
        }
        
        if (record.getName() != null) {
            sql.SET("name = #{name,jdbcType=VARCHAR}");
        }
        
        if (record.getMobile() != null) {
            sql.SET("mobile = #{mobile,jdbcType=VARCHAR}");
        }
        
        if (record.getEmail() != null) {
            sql.SET("email = #{email,jdbcType=VARCHAR}");
        }
        
        if (record.getImgPath() != null) {
            sql.SET("img_path = #{imgPath,jdbcType=VARCHAR}");
        }
        
        if (record.getLastLogin() != null) {
            sql.SET("last_login = #{lastLogin,jdbcType=TIMESTAMP}");
        }
        
        if (record.getDiscTitle() != null) {
            sql.SET("disc_title = #{discTitle,jdbcType=VARCHAR}");
        }
        
        if (record.getDiscDetail() != null) {
            sql.SET("disc_detail = #{discDetail,jdbcType=VARCHAR}");
        }
        
        if (record.getRegisterTime() != null) {
            sql.SET("register_time = #{registerTime,jdbcType=TIMESTAMP}");
        }
        
        sql.WHERE("id = #{id,jdbcType=INTEGER}");
        
        return sql.toString();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table cscp_user_detail
     *
     * @mbg.generated Mon Apr 23 08:56:34 CST 2018
     */
    protected void applyWhere(SQL sql, CscpUserDetailExample example, boolean includeExamplePhrase) {
        if (example == null) {
            return;
        }
        
        String parmPhrase1;
        String parmPhrase1_th;
        String parmPhrase2;
        String parmPhrase2_th;
        String parmPhrase3;
        String parmPhrase3_th;
        if (includeExamplePhrase) {
            parmPhrase1 = "%s #{example.oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{example.oredCriteria[%d].allCriteria[%d].value} and #{example.oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{example.oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{example.oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{example.oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        } else {
            parmPhrase1 = "%s #{oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{oredCriteria[%d].allCriteria[%d].value} and #{oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        }
        
        StringBuilder sb = new StringBuilder();
        List<Criteria> oredCriteria = example.getOredCriteria();
        boolean firstCriteria = true;
        for (int i = 0; i < oredCriteria.size(); i++) {
            Criteria criteria = oredCriteria.get(i);
            if (criteria.isValid()) {
                if (firstCriteria) {
                    firstCriteria = false;
                } else {
                    sb.append(" or ");
                }
                
                sb.append('(');
                List<Criterion> criterions = criteria.getAllCriteria();
                boolean firstCriterion = true;
                for (int j = 0; j < criterions.size(); j++) {
                    Criterion criterion = criterions.get(j);
                    if (firstCriterion) {
                        firstCriterion = false;
                    } else {
                        sb.append(" and ");
                    }
                    
                    if (criterion.isNoValue()) {
                        sb.append(criterion.getCondition());
                    } else if (criterion.isSingleValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase1, criterion.getCondition(), i, j));
                        } else {
                            sb.append(String.format(parmPhrase1_th, criterion.getCondition(), i, j,criterion.getTypeHandler()));
                        }
                    } else if (criterion.isBetweenValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase2, criterion.getCondition(), i, j, i, j));
                        } else {
                            sb.append(String.format(parmPhrase2_th, criterion.getCondition(), i, j, criterion.getTypeHandler(), i, j, criterion.getTypeHandler()));
                        }
                    } else if (criterion.isListValue()) {
                        sb.append(criterion.getCondition());
                        sb.append(" (");
                        List<?> listItems = (List<?>) criterion.getValue();
                        boolean comma = false;
                        for (int k = 0; k < listItems.size(); k++) {
                            if (comma) {
                                sb.append(", ");
                            } else {
                                comma = true;
                            }
                            if (criterion.getTypeHandler() == null) {
                                sb.append(String.format(parmPhrase3, i, j, k));
                            } else {
                                sb.append(String.format(parmPhrase3_th, i, j, k, criterion.getTypeHandler()));
                            }
                        }
                        sb.append(')');
                    }
                }
                sb.append(')');
            }
        }
        
        if (sb.length() > 0) {
            sql.WHERE(sb.toString());
        }
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table cscp_user_detail
     *
     * @mbg.generated Mon Apr 23 08:56:34 CST 2018
     */
    public String selectByExampleWithPage(CscpUserDetailExample example) {
        String sql = selectByExample(example);
        return sql;
    }
    
	public String queryUserDetail(CscpUserDetailLike cscpUserDetailLike, String orderby) {
		SQL sql = new SQL();
		sql.SELECT("cscp_user.username");
		sql.SELECT("cscp_user_detail.id as id");
		sql.SELECT("cscp_user_detail.user_id");
		sql.SELECT("cscp_user_detail.family_name");
		sql.SELECT("cscp_user_detail.name");
		sql.SELECT("cscp_user_detail.mobile");
		sql.SELECT("cscp_user_detail.email");
		sql.SELECT("cscp_user_detail.img_path");
		sql.SELECT("cscp_user_detail.last_login");
		sql.SELECT("cscp_user_detail.disc_title");
		sql.SELECT("cscp_user_detail.disc_detail");
		sql.SELECT("cscp_user_detail.register_time");
		sql.FROM("cscp_user");
		sql.LEFT_OUTER_JOIN("cscp_user_detail on cscp_user.id=cscp_user_detail.user_id");

		StringBuilder or = new StringBuilder("");
		boolean isFirst = true;
		if(StringUtils.isNotBlank(cscpUserDetailLike.getUsernameLike())) {
			or.append(" cscp_user.username like '%"+cscpUserDetailLike.getUsernameLike()+"%' ");
			isFirst = false;
		}
		if(StringUtils.isNotBlank(cscpUserDetailLike.getFamilyNameLike())) {
			if(!isFirst) {
				or.append(" or ");
			}
			or.append(" cscp_user_detail.family_name like '%"+cscpUserDetailLike.getFamilyNameLike()+"%' ");
			isFirst = false;
		}
		if(StringUtils.isNotBlank(cscpUserDetailLike.getNameLike())) {
			if(!isFirst) {
				or.append(" or ");
			}
			or.append(" cscp_user_detail.name like '%"+cscpUserDetailLike.getNameLike()+"%' ");
			isFirst = false;
		}
		if(StringUtils.isNotBlank(cscpUserDetailLike.getMobileLike())) {
			if(!isFirst) {
				or.append(" or ");
			}
			or.append(" cscp_user_detail.mobile like '%"+cscpUserDetailLike.getMobileLike()+"%' ");
			isFirst = false;
		}
		if(StringUtils.isNotBlank(cscpUserDetailLike.getEmailLike())) {
			if(!isFirst) {
				or.append(" or ");
			}
			or.append(" cscp_user_detail.email like '%"+cscpUserDetailLike.getEmailLike()+"%' ");
			isFirst = false;
		}
		if(StringUtils.isNotBlank(cscpUserDetailLike.getDiscTitleLike())) {
			if(!isFirst) {
				or.append(" or ");
			}
			or.append(" cscp_user_detail.disc_title like '%"+cscpUserDetailLike.getDiscTitleLike()+"%' ");
			isFirst = false;
		}
		if(StringUtils.isNotBlank(cscpUserDetailLike.getDiscDetailLike())) {
			if(!isFirst) {
				or.append(" or ");
			}
			or.append(" cscp_user_detail.disc_detail like '%"+cscpUserDetailLike.getDiscDetailLike()+"%' ");
		}

        if(StringUtils.isNotBlank(or.toString())) {
            sql.WHERE(or.toString());
        }
        
		if(StringUtils.isNotBlank(orderby)) {
			sql.ORDER_BY(orderby);
		}
		
		return sql.toString();
	}


    public String queryUserDetailPreventSQLAttack( String usernameLike,
                                                   String familyNameLike,
                                                   String nameLike,
                                                   String mobileLike,
                                                   String emailLike,
                                                   String discTitleLike,
                                                   String discDetailLike,
                                                   String orderby) {
        SQL sql = new SQL();
        sql.SELECT("cscp_user.username");
        sql.SELECT("cscp_user_detail.id as id");
        sql.SELECT("cscp_user_detail.user_id");
        sql.SELECT("cscp_user_detail.family_name");
        sql.SELECT("cscp_user_detail.name");
        sql.SELECT("cscp_user_detail.mobile");
        sql.SELECT("cscp_user_detail.email");
        sql.SELECT("cscp_user_detail.img_path");
        sql.SELECT("cscp_user_detail.last_login");
        sql.SELECT("cscp_user_detail.disc_title");
        sql.SELECT("cscp_user_detail.disc_detail");
        sql.SELECT("cscp_user_detail.register_time");
        sql.FROM("cscp_user");
        sql.LEFT_OUTER_JOIN("cscp_user_detail on cscp_user.id=cscp_user_detail.user_id");

        StringBuilder or = new StringBuilder("");
        boolean isFirst = true;
        if(StringUtils.isNotBlank(usernameLike)) {
            or.append(" cscp_user.username like concat('%','"+usernameLike+"','%') ");
            isFirst = false;
        }
        if(StringUtils.isNotBlank(familyNameLike)) {
            if(!isFirst) {
                or.append(" or ");
            }
            or.append(" cscp_user_detail.family_name like  concat('%','"+familyNameLike+"','%') ");
            isFirst = false;
        }
        if(StringUtils.isNotBlank(nameLike)) {
            if(!isFirst) {
                or.append(" or ");
            }
            or.append(" cscp_user_detail.name like concat('%','"+nameLike+"','%') ");
            isFirst = false;
        }
        if(StringUtils.isNotBlank(mobileLike)) {
            if(!isFirst) {
                or.append(" or ");
            }
            or.append(" cscp_user_detail.mobile like concat('%','"+mobileLike+"','%') ");
            isFirst = false;
        }
        if(StringUtils.isNotBlank(emailLike)) {
            if(!isFirst) {
                or.append(" or ");
            }
            or.append(" cscp_user_detail.email like concat('%','"+emailLike+"','%') ");
            isFirst = false;
        }
        if(StringUtils.isNotBlank(discTitleLike)) {
            if(!isFirst) {
                or.append(" or ");
            }
            or.append(" cscp_user_detail.disc_title like concat('%','"+discTitleLike+"','%') ");
            isFirst = false;
        }
        if(StringUtils.isNotBlank(discDetailLike)) {
            if(!isFirst) {
                or.append(" or ");
            }
            or.append(" cscp_user_detail.disc_detail like concat('%','"+discDetailLike+"','%') ");
        }

        if(StringUtils.isNotBlank(or.toString())) {
            sql.WHERE(or.toString());
        }

        if(StringUtils.isNotBlank(orderby)) {
            sql.ORDER_BY(orderby);
        }

        return sql.toString();
    }


	public String queryUserDetailByUserId(String userId, String orderby) {
		StringBuilder sql = new StringBuilder();
		sql.append("select a.username, b.id as id, b.user_id, "
				+ "family_name, name, mobile, email, img_path, last_login, "
				+ "disc_title, disc_detail, register_time "
				+ "from cscp_user a, cscp_user_detail b "
				+ "where a.id = b.user_id ");
		
		if(StringUtils.isNotBlank(userId)) {
			sql.append(" and a.id = "+userId+" ");
		}
		
		if(StringUtils.isNotBlank(orderby)) {
			sql.append(" order by "+orderby);
		}
		
		return sql.toString();
	}
	
}